import { Output, Input, EventEmitter, ChangeDetectorRef, ViewChild } from '@angular/core';
import { MdcCard } from '@angular-mdc/web';
import { has } from 'ramda';

import { Control, DashboardType } from '../../dashboard-model';

export abstract class Base {

  @ViewChild(MdcCard) cardRef: MdcCard; 

  @Output() remove = new EventEmitter<any>();

  @Output() openPanel = new EventEmitter<Control>();

  @Input() set control(control: Control) {
    if (!control) {
      return;
    }

    if (has('options', control) && !(control.options instanceof Object)) {
      control.options = JSON.parse(control.options);
    }
    
    this._control = control;
    this.changeDetectorRef.markForCheck();
  };

  _control: Control;

  @Input() set dashboardType(type: DashboardType) {
    if (!type) {
      return;
    }

    this._dashboardType = type;
    this.changeDetectorRef.markForCheck();
  }

  _dashboardType: DashboardType = DashboardType.Renderer;

  get isRenderer() {
    return this._dashboardType === DashboardType.Renderer;
  }

  constructor(
    public changeDetectorRef: ChangeDetectorRef,
  ) { }

  removeControl() {
    this.remove.emit(this._control);
  }

  preventDefault(event: Event) {
    event.preventDefault();
    event.stopPropagation();
  }

  openControlPanel() {
    this.openPanel.emit(this._control);
  }
}
